package com.adv.crm.interceptor;

import com.adv.crm.beans.UserBean;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 16978 on 2016/8/4.
 */
public class CommonInterceptor extends HandlerInterceptorAdapter {
    private static final String LOGIN_URL = "/admin/user/login";
    private static final String LOGIN_JSON_URL = "/admin/user/json/login";
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!request.getRequestURI().startsWith(LOGIN_URL) && !request.getRequestURI().startsWith(LOGIN_JSON_URL)) {
            UserBean user = (UserBean) request.getSession().getAttribute("user");
            if (user != null){
                //check role
            }else {
                response.sendRedirect(LOGIN_URL);
            }
        }

        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }
}
